<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;
use think\Db;

class NodeModel extends Model
{
    // 确定链接表名
    protected $name = 'adminnode';

    /**
     * 获取节点数据
     */
    public function getNodeInfo($id)
    {
        $result = $this->field('_id,node_name,type_id')->select();
        $str = '';
        $role = new RoleModel();
        $rule = $role->getRuleById($id);
        if (!empty($rule)) {
            $rule = explode(',', $rule);
        }
        foreach ($result as $key => $vo) {
            $vo = objToArray($vo);
            $str .= '{ "id": "' . $vo['_id']['$oid'] . '", "pId":"' . $vo['type_id'] . '", "name":"' . $vo['node_name'] . '"';
            if (!empty($rule) && in_array($vo['_id']['$oid'], $rule)) {
                $str .= ' ,"checked":1';
            }
            $str .= '},';
        }
        return '[' . rtrim($str, ',') . ']';
    }

    /**
     * 根据节点数据获取对应的菜单
     * @param $nodeStr  用户拥有的权限节点数组（_id)
     */
    public function getMenu($nodeStr = '')
    {
        if (empty($nodeStr)) {
            return [];
        }
        // 超级管理员没有节点数组 * 号表示  is_menu 1 不是菜单选项    2 是菜单选项
        // $where = '*' == $nodeStr ? 'is_menu = 2' : 'is_menu = 2 and id in(' . $nodeStr . ')';
        if ($nodeStr == "*") {
            $where['is_menu'] = '2';
        } else {
            $where['is_menu'] = '2';
            $where['_id'] = ['in', ". $nodeStr ."];
            // $where = 'is_menu = 2 and _id in(' . $nodeStr . ')';
        }
        $result = $this->where($where)->select();
        $menu = prepareMenu($result);
        return $menu;
    }

    /**
     * 根据条件获取访问权限节点数据
     * @param $where
     */
    public function getActions($where)
    {
        //var_dump($where);die;
        $data =  $this->field('control_name,action_name')->select();
        // echo "aaaaaaaaaaaaaaaaa<br> \n";
        // echo DB::name('adminnode')->getlastsql();
        // echo "aaaaaaaaaaaaaaaaa<br> \n";
        // die;
        // var_dump($data);die;
        return $data;
        // return $this->field('control_name,action_name')->where($where)->select();
    }

    /**
     * 获取节点数据
     * @return mixed
     */
    public function getNodeList()
    {
        // die('aaaaaa');
        $data = $this->select();
        //var_dump($data);die;
        return $data;
    }

    /**
     * 插入节点信息
     * @param $param
     */
    public function insertNode($param)
    {
        try {
            $this->save($param);
            return msgs(1, '', '添加节点成功');
        } catch (PDOException $e) {
            return msgs(-2, '', $e->getMessage());
        }
    }

    /**
     * 编辑节点
     * @param $param
     */
    public function editNode($param)
    {
        try {
            // var_dump($param);
            $this->save($param, ['_id' => $param['id']]);
            return msgs(1, '', '编辑节点成功');
        } catch (PDOException $e) {
            return msgs(-2, '', $e->getMessage());
        }
    }

    /**
     * 删除节点
     * @param $id
     */
    public function delNode($id)
    {
        try {
            $this->where('_id', $id)->delete();
            return msgs(1, '', '删除节点成功');
        } catch (PDOException $e) {
            return msgs(-1, '', $e->getMessage());
        }
    }
}
